<template>
    <div class="wrapper">
        <v-head :logoimg="updatelogoimg"></v-head>
        <v-sidebar></v-sidebar>
        <div class="content-box" :class="{'content-collapse':collapse}">
            <v-tags :updateindex="updateindex"></v-tags>
            <div class="content">
                <transition name="move" mode="out-in">
                    <keep-alive :include="tagsList">
                        <router-view :test="tagsList" @updateclass="updatetagslist" @updateImg="updateImg"></router-view>
                    </keep-alive>
                </transition>
                <el-backtop target=".content" ></el-backtop>
            </div>
        </div>
    </div>
</template>

<script>
import vHead from './Header.vue';
import vSidebar from './Sidebar.vue';
import vTags from './Tags.vue';
import bus from './bus';
export default {
    data() {
        return {
            tagsList: [],
            collapse: false,
            updateindex: -1,
            updatelogoimg: '',
        };
    },
    components: {
        vHead,
        vSidebar,
        vTags
    },
    methods: {
        /**
         * 监听updateClass组件的发射事件
         * 获取传输的下标，然后传递给tags
         */
        updatetagslist(index) {
            console.log("监听组件事件，下标")
            console.log(index)
            this.updateindex = index
            // 延时设置updateindex恢复默认值
            // 防止两次传输的值一样
            // 导致tags监听不到值的改变
            setTimeout(() => {
                this.updateindex = -1
            }, 1000)
        },
        updateImg() {
            console.log("头像已修改");
            this.updatelogoimg = localStorage.getItem("ms_logoimg")
            console.log(this.updatelogoimg)
        }
    },
    created() {
        bus.$on('collapse-content', msg => {
            this.collapse = msg;
        });

        // 只有在标签页列表里的页面才使用keep-alive，即关闭标签之后就不保存到内存中了。
        bus.$on('tags', msg => {
            let arr = [];
            for (let i = 0, len = msg.length; i < len; i++) {
                msg[i].name && arr.push(msg[i].name);
            }
            this.tagsList = arr;
        });
        
        this.updatelogoimg = localStorage.getItem("ms_logoimg")
        console.log(this.updatelogoimg)
    }
};
</script>
